package com.atguigu.auth.controller;


import com.atguigu.auth.service.SysUserService;
import com.atguigu.common.result.Result;
import com.atguigu.common.until.MD5;
import com.atguigu.model.system.SysUser;
import com.atguigu.vo.system.SysUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-03-09
 */

@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService ;

    //用户分页条件查询
    @ApiOperation("获取用户列表")
    @GetMapping("{page}/{limit}")
    //sysUserQueryVo 条件对象
    public Result index(@PathVariable Long page,@PathVariable Long limit,SysUserQueryVo sysUserQueryVo) {
        //创建分页对象
        Page<SysUser> pageInfo = new Page<>(page, limit);
        //使用条件查询器进行查询
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();

        String username = sysUserQueryVo.getKeyword();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();

        //使用模糊查询来进行条件查询
        if(!StringUtils.isEmpty(username)){
            queryWrapper.like(SysUser :: getUsername, username);
        }
        if(!StringUtils.isEmpty(createTimeBegin)){
            queryWrapper.ge(SysUser :: getCreateTime, createTimeBegin); //大于等于
        }
        if(!StringUtils.isEmpty(createTimeEnd)){
            queryWrapper.le(SysUser :: getCreateTime, createTimeEnd); //小于等于  lt是小于
        }

        IPage<SysUser> page1 = sysUserService.page(pageInfo, queryWrapper);
        return Result.ok(page1);
    }
    @ApiOperation(value = "更新状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        sysUserService.updateStatus(id, status);
        return Result.ok();
    }
    //获取用户
    @ApiOperation("获取用户")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        SysUser user = sysUserService.getById(id);
        return Result.ok(user);
    }
    //保存用户
    @ApiOperation(value = "保存用户")
    @PostMapping("save")
    public Result save(@RequestBody SysUser user) {
        //密码进行加密存储
        String encrypt = MD5.encrypt(user.getPassword());
        //将加密后的密码保存到对象中
        user.setPassword(encrypt);
        //执行SQL语句
        sysUserService.save(user);
        return Result.ok();
    }
    @ApiOperation(value = "更新用户")
    @PutMapping("update")
    public Result updateById(@RequestBody SysUser user) {
        sysUserService.updateById(user);
        return Result.ok();
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        sysUserService.removeById(id);
        return Result.ok();
    }
}

